<template>
  <div class="wrapper">
    <header-view></header-view>
    <div class="banner">
      <img src="~img/bg2.jpg" alt="" />
    </div>
    <div class="cate-view" v-if="cateList.length">
      <div
        class="cate"
        v-for="(item, index) of cateList"
        :key="index"
        :class="{ active: index === cateIndex }"
        @click="clickCate(index)"
      >
        {{ item.cate_name }}
        <span v-show="index === cateIndex"></span>
      </div>
    </div>
    <div class="main">
      <div class="swiper-view" v-if="carousel.length">
        <swiper :options="swiperOptions" ref="swiper">
          <swiper-slide class="slide" v-for="(item, index) of carousel" :key="index">
            <article-swiper-item :info="item"></article-swiper-item>
          </swiper-slide>
        </swiper>
        <div class="swiper-page">
          <div :class="{ active: index === swiperIndex }" v-for="(item, index) of carousel" :key="index"></div>
        </div>
      </div>
      <div class="search-view">
        <div class="left">
          <div class="btn">
            {{ year || '年份' }}<span class="icon4"></span>
            <el-date-picker v-model="yearDate" type="year" @change="yearChange"> </el-date-picker>
          </div>
          <div class="btn">
            {{ month + '月' || '月份' }}<span class="icon4"></span>
            <el-date-picker v-model="monthDate" type="month" @change="monthChange"> </el-date-picker>
          </div>
          <div class="btn input">
            <input type="text" placeholder="请输入关键字" v-model="keyword" v-on:keyup="handleKeyUp" />
            <div class="click" @click="clickSearch"><span class="icon5"></span></div>
          </div>
        </div>
        <div class="right">
          <div class="btn" :class="{ active: listStyle === 0 }" @click="listStyle = 0">图文展示</div>
          <div class="btn" :class="{ active: listStyle === 1 }" @click="listStyle = 1">文本展示</div>
        </div>
      </div>
      <div class="list" v-if="this.listTotal">
        <ul class="list-float" v-show="listStyle === 0">
          <li v-for="(item, index) of list" :key="index">
            <article-float-item :info="item"></article-float-item>
          </li>
        </ul>
        <ul class="list-vertical" v-show="listStyle === 1">
          <li v-for="(item, index) of list" :key="index">
            <article-vertical-item :info="item"></article-vertical-item>
          </li>
        </ul>
        <div class="style-fixed">
          <img src="~img/list-style0.png" v-show="listStyle === 1" @click="listStyle = 0" />
          <img src="~img/list-style1.png" v-show="listStyle === 0" @click="listStyle = 1" />
        </div>
      </div>
      <div class="no-list" v-else>
        <el-empty :image-size="200"></el-empty>
      </div>
      <div class="pagination-view" v-if="this.listTotal">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="listTotal"
          :page-size="perpage"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>
    </div>
    <bottom-view></bottom-view>
  </div>
</template>

<script>
import { articleCateApi, articleListApi } from '@/api/api'
import 'swiper/dist/css/swiper.css'
import { swiper, swiperSlide } from 'vue-awesome-swiper'
import articleSwiperItem from '@/components/articleSwiperItem'
import articleFloatItem from '@/components/articleFloatItem'
import articleVerticalItem from '@/components/articleVerticalItem'
import headerView from '@/components/headerView'
import bottomView from '@/components/bottomView'
export default {
  name: 'articleList',
  components: {
    swiper,
    swiperSlide,
    articleSwiperItem,
    articleFloatItem,
    articleVerticalItem,
    headerView,
    bottomView
  },
  data() {
    return {
      id: '',
      cateIndex: 0,
      cateList: [],
      carousel: [],
      swiperIndex: 0,
      swiperOptions: {
        effect: 'fade',
        loop: false,
        autoplay: false,
        on: {
          slideChange: () => {
            this.swiperIndex = this.$refs.swiper.swiper.realIndex
          }
        }
      },
      listStyle: 0,
      page: 1,
      perpage: 9,
      listTotal: 0,
      year: '',
      yearDate: '',
      month: '',
      monthDate: '',
      keyword: '',
      list: [],
      loadFinish: false
    }
  },
  mounted() {
    this.getCate()
  },
  methods: {
    async getCate() {
      const { errno, data } = await articleCateApi({
        article_cate_id: this.$route.query.id
      })
      if (errno === 0) {
        this.cateList = data
        this.id = this.$route.query.id
        this.getInfo()
      }
    },
    clickCate(i) {
      if (this.cateIndex != i) this.cateIndex = i
      this.getInfo()
    },
    clickSearch() {
      this.page = 1
      this.getInfo()
    },
    handleKeyUp(event) {
      if (event.keyCode === 13) this.clickSearch()
    },
    async getInfo() {
      const { errno, data } = await articleListApi({
        article_cate_id: this.cateList.length ? this.cateList[this.cateIndex].article_cate_id : this.id,
        page: this.page,
        perpage: this.perpage,
        keywords: this.keyword,
        month: this.month,
        year: this.year
      })
      if (errno === 0) {
        this.loadFinish = true
        this.carousel = data.carousel.map(item => {
          item.add_time = this.COMMON.handleTime(item.add_time, 'date')
          return item
        })
        this.listTotal = data.list.total
        this.list = data.list.data.map(item => {
          item.add_time = this.COMMON.handleTime(item.add_time, 'date')
          return item
        })
      }
    },
    handleCurrentChange(page) {
      this.page = page
      this.getInfo()
    },
    yearChange(e) {
      this.yearDate = e
      let date = new Date(e)
      this.year = date.getFullYear()
      this.clickSearch()
    },
    monthChange(e) {
      this.yearDate = e
      this.monthDate = e
      let date = new Date(e)
      this.year = date.getFullYear()
      this.month = date.getMonth() + 1
      this.clickSearch()
    }
  }
}
</script>

<style lang="less" scoped>
.banner {
  height: 640px;
  width: 100%;
  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
}
.cate-view {
  background: #fff;
  height: 126px;
  line-height: 126px;
  text-align: center;
  .cate {
    display: inline-block;
    font-size: 30px;
    margin-right: 170px;
    position: relative;
    cursor: pointer;
    &:last-child {
      margin-right: 0;
    }
    &.active {
      color: @base-color;
      span {
        width: 77px;
        height: 4px;
        background: #994b1a;
        border-radius: 20px;
        position: absolute;
        bottom: -2px;
        left: 50%;
        transform: translateX(-50%);
      }
    }
  }
}
.main {
  width: 1264px;
  margin: 0 auto;
  padding-bottom: 80px;
  .swiper-view {
    margin-top: 99px;
    width: 100%;
    height: 295px;
    background: #ffffff;
    position: relative;
    box-shadow: 0px 5px 10px 1px rgba(149, 149, 149, 0.16);
    .slide {
    }
    .swiper-page {
      position: absolute;
      width: 100%;
      left: 0;
      bottom: -40px;
      height: 11px;
      text-align: center;
      div {
        display: inline-block;
        width: 11px;
        height: 11px;
        background: #777777;
        border-radius: 50%;
        margin: 0 6px;
        &.active {
          background: @base-color;
        }
      }
    }
  }
  .search-view {
    overflow: hidden;
    margin-top: 132px;
    padding-bottom: 50px;
    .left {
      float: left;
      .btn {
        display: inline-block;
        width: 196px;
        height: 40px;
        line-height: 38px;
        border-radius: 24px;
        border: 1px solid #d9d9d9;
        margin-right: 30px;
        color: #808080;
        font-size: 14px;
        padding-left: 15px;
        padding-right: 22px;
        cursor: pointer;
        position: relative;
        transition: all 0.3s;
        &.input {
          width: 392px;
          margin-right: 0px;
          padding-right: 0px;
          input {
            width: 320px;
            background: transparent;
            color: #808080;
            line-height: 30px;
            height: 30px;
            vertical-align: top;
            margin-top: 4px;
            &::placeholder {
              color: #808080;
            }
          }
          .click {
            width: 50px;
            height: 38px;
            float: right;
            position: relative;
          }
        }
        .icon4 {
          position: absolute;
          right: 22px;
          top: 50%;
          transform: translateY(-50%);
        }
        .icon5 {
          position: absolute;
          left: 50%;
          top: 50%;
          transform: translate(-50%, -50%);
        }
        &:hover {
          border-color: @base-color;
        }
        /deep/ .el-date-editor {
          position: absolute; //绝对定位
          top: 0;
          left: 0;
          opacity: 0; //设置完全透明
        }
      }
    }
    .right {
      float: right;
      .btn {
        display: inline-block;
        width: 128px;
        height: 40px;
        line-height: 38px;
        border-radius: 24px;
        border: 1px solid #dcdcdc;
        margin-right: 30px;
        color: #808080;
        font-size: 18px;
        text-align: center;
        background: #ffffff;
        cursor: pointer;
        transition: all 0.3s;
        &:last-child {
          margin-right: 0;
        }
        &.active {
          color: @base-color;
          border-color: @base-color;
        }
      }
    }
  }
  .list {
    position: relative;
    .style-fixed {
      position: absolute;
      top: 50%;
      right: -223px;
      transform: translateY(-50%);
      width: 173px;
      height: 63px;
      cursor: pointer;
      transition: all 0.3s;
      &:hover {
        right: -233px;
      }
      img {
        width: 100%;
        height: 100%;
        display: block;
      }
    }
    .list-float {
      overflow: hidden;

      li {
        float: left;
        margin-right: 47px;
        margin-bottom: 40px;
        &:nth-child(3n) {
          margin-right: 0;
        }
      }
    }
    .list-vertical {
      overflow: hidden;
      background: #ffffff;
      padding: 20px 20px 96px 20px;
      li {
        border-bottom: 2px dashed #e5e5e5;
      }
    }
  }
  .no-list {
    padding: 100px 0;
  }
  .pagination-view {
    text-align: center;
    padding: 63px 0 63px 0;
  }
}
</style>
